package com.hd.service.impl;

import com.hd.dao.BaseDao;
import com.hd.entity.Todo;
import com.hd.service.TodoService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 待办事项
 */
@Service("todoService")

public class TodoServiceImpl implements TodoService {
    @Autowired
    private BaseDao<Todo,String>todoDao;
    @Override
    public void save(Todo todo) {
        todo.setId(StringUtils.getUUId());
        todoDao.save(todo);
    }

    /**
     * 更新
     * @param objId
     * @param status
     */
    @Override
    public void update(String objId, Integer status,String type) {
        String hql="update Todo set status=? where objId=? and type=?";
        todoDao.execute(hql,status,objId,type);
    }

    @Override
    public void deleteByParams(String objId, String type) {
        String hql="delete from  Todo  where objId=? and type=?";
        todoDao.execute(hql,objId,type);
    }

    @Override
    public void deleteByParams(String objId, String type,String userid) {
        String hql="delete from  Todo  where objId=? and type=? and userid=?";
        todoDao.execute(hql,objId,type,userid);
    }

    /**
     * 根据Ids批量更新
     * @param ids
     * @param status
     */
    @Override
    public void updateByIdsAndStatus(String ids, Integer status) {
        String hql="update Todo set status=? where id in("+ StringUtils.convertSingleStr(ids)+")";
        todoDao.execute(hql,status,ids);
    }

    /**
     * 分页查询待办
     * @param info
     * @param todo
     */
    @Override
    public void selectDataGrid(PageInfo info, Todo todo) {
        Map<String,Object> condition=info.getCondition();
        List<Object> params=new ArrayList<Object>();
        String hql="from Todo where 1=1";
        if(StringUtils.isNotNull(todo.getStatus())){
            hql+=" and status=?";
            params.add(todo.getStatus());
        }
        if(StringUtils.isNotNull(todo.getUserId())){
            hql+=" and userId=?";
            params.add(todo.getUserId());
        }
        if(StringUtils.isNotNull(todo.getTitle())){
            hql+=" and title like ?";
            params.add("%"+todo.getTitle().trim()+"%");
        }
        if(StringUtils.isNotNull(condition.get("createTimeStart"))){
            hql+=" and createTime>=?";
            params.add(condition.get("createTimeStart"));
        }
        if(StringUtils.isNotNull(condition.get("createTimeEnd"))){
            hql+=" and createTime<=?";
            params.add(condition.get("createTimeEnd"));
        }
        hql+=" order by createTime desc";
        String countHql="select count(*) "+hql;
        List<Todo>todoList=todoDao.findPage(hql,info.getNowpage(),info.getPagesize(),params);
        Long count=todoDao.getCountByList(countHql,params);
        info.setRows(todoList);
        info.setTotal(Integer.parseInt(count+""));
    }
}
